<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
    <template v-slot:code_unique_func="{row}">
      <input type="hidden" :name="row.name" :value="func_value">
      <select_one config="组序列|index,总序列|id" v-model:value="row.v1"></select_one>
      <checkbox config="序列首位为1|1,36进制|zip,混淆的|confuse" v-model:value="row.v2"></checkbox>
    </template>
  </forms>
</template>

<script>
import {ajax} from "@/js/api_tool.js";
import {iframe} from "@/js/tool.js";
import select_one from "@/components/auto_import/UnifyParam/select_one.vue";
import checkbox from "@/components/auto_import/UnifyParam/checkbox.vue";
import {_config} from "@/js/s94_tool.js";
import s94Mjs from "s94-js";
import {vue_template} from "@/js/dom_tool.js";


export default {
  components: {select_one, checkbox},
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    var status_config = _config('status_alias_map.code_group.status');
    let forms_config = {
      title: {label:'标题',name:'title',type:'text_one',tips:''},
      code_prefix: {label:'兑换码前缀',name:'code_prefix',type:'text_one',tips:'只允许小写的英文字母和数字'},
      code_unique_func: {label:'序列码模板',name:'code_unique_func',type:'code_unique_func',tips:'',v1:'index',v2:''},
      code_unique_length: {label:'序列码长度',name:'code_unique_length',type:'text_one',value:6,config:'number',tips:'长度设定范围：1-10'},
      code_upper: {label:'兑换码大写',name:'code_upper',type:'radio_switch',tips:'',value:0},
      execute_id: {name:'execute_id', label:'兑换内容', type:'list_id', config:'/execute/index?model_name=code'},
      status: {label:'状态',name:'status',type:'radio_switch',config:status_config,value:1},
    }
    if (id){
      forms_config.create_time = {label:'创建时间',name:'create_time',tips:''};
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,
      function_map: [],
    }
  },
  methods: {
    onsubmit(formData){
      let url = this.id ? `/admin/code_group/edit?id=`+this.id : '/admin/code_group/add';
      // if (!Object.keys(condition).length) return this.$msgbox({message:'兑换内容不能为空'});
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('code_group/index', '$refs.list_url.list');
          iframe.hide('code_group/edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    },
  },
  computed: {
    func_value(){
      let v1 = this.config.code_unique_func.v1 || '';
      let v2 = this.config.code_unique_func.v2 || '';
      let arr = v2 ? v2.split(',') : [];
      arr.sort();
      return v1+(arr.length ? '_'+arr.join('_') : '');
    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/code_group/info?id='+this.id, cache:true}, (res)=>{
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('code_group/edit');
        }else {
          this.$router.push('index');
        }
      })
    }

    //其他初始化操作
  }
}
</script>
